package com.huluobo.a2203kotlinshopping.detail

import android.content.Context
import android.content.Intent
import android.graphics.Color
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.widget.ImageView
import com.blankj.utilcode.util.SPUtils
import com.blankj.utilcode.util.ToastUtils
import com.huluobo.a2203kotlinshopping.R
import com.huluobo.a2203kotlinshopping.databinding.ActivityDetailBinding
import com.huluobo.a2203kotlinshopping.login.LoginActivity
import com.huluobo.a2203kotlinshopping.service.ServiceActivity
import com.huluobo.a2203kotlinshopping.utils.ImageUtils
import com.huluobo.base.net.Const
import com.huluobo.base.view.BaseMVVMActivity
import com.youth.banner.BannerConfig
import com.youth.banner.loader.ImageLoader

class DetailActivity : BaseMVVMActivity<ActivityDetailBinding, DetailViewModel>() {
    private var goodsId = 0
    override fun getViewModel(): Class<DetailViewModel> = DetailViewModel::class.java

    override fun initData() {
        goodsId = intent.getIntExtra(Const.PARAM_GOODS_ID, 0)
        viewModel.getGoodsDetail(goodsId)

        viewModel.getGoodsDetailSuccess.observe(this) {
            if (it.code == 200) {
                dataBinding.goods = it.data
                dataBinding.detailBanner.setImages(it.data.bannerList)
                dataBinding.detailBanner.start()

            } else {
                ToastUtils.showLong(it.message)
            }
        }

        viewModel.getGoodsDetailFailed.observe(this) {
            Log.i("TagA", "获取商品详情失败:$it")
        }

        viewModel.addToCartSuccess.observe(this) {
            ToastUtils.showLong(it.message)
        }

        viewModel.addToCartFailed.observe(this) {
            Log.i("TagA", "添加购物车失败:$it")
        }
    }

    override fun initView() {
        window.statusBarColor = Color.TRANSPARENT
        //初始化banner
        dataBinding.detailBanner.setBannerStyle(BannerConfig.CIRCLE_INDICATOR)//设置指示器风格
        dataBinding.detailBanner.setImageLoader(object : ImageLoader() {
            //设置banner的图片加载器
            override fun displayImage(context: Context?, path: Any?, imageView: ImageView?) {
                imageView.let {
                    if (context != null && path != null) {
                        ImageUtils.loadImage(context, path.toString(), it!!)
                    }
                }
            }
        })
        dataBinding.detailBanner.setDelayTime(2000)//设置banner播放时间
        dataBinding.detailBanner.setIndicatorGravity(BannerConfig.CENTER)

        dataBinding.detailBackIv.setOnClickListener {
            finish()
        }

        dataBinding.detailAddToCart.setOnClickListener {
            if (SPUtils.getInstance().getString(Const.PARAM_TOKEN).isNotEmpty()) {
                viewModel.addToCart(goodsId, 1)
            } else {
                val intent = Intent(this, LoginActivity::class.java)
                startActivity(intent)
            }
        }

        dataBinding.detailServer.setOnClickListener {
            val intent = Intent(this, ServiceActivity::class.java)
            val bundle = Bundle()
            bundle.putString("conversationId", "kefu")
            bundle.putInt("chatType", 1)
            intent.putExtras(bundle)
            startActivity(intent)
        }
    }

    override fun getLayoutId(): Int = R.layout.activity_detail
}